package com.geralt.xmlparse.connect;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;

import com.geralt.xmlparse.entrance.SendEntrance;

public class DBUtil2 {

	private static BasicDataSource dataSource;

	static {
		Properties props = new Properties();
		InputStream is = SendEntrance.class.getClassLoader().getResourceAsStream(
				"jdbc.properties");
		try {
			props.load(is);

		} catch (Exception e) {
			e.printStackTrace();
		}
		dataSource = null;
		try {
			dataSource = new BasicDataSourceFactory().createDataSource(props);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Connection getConnection() {
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void free(){
		try {
			dataSource.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
}
